//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

using Microsoft.Research.ScientificWorkflow;
using System.Threading;
using System.Data.Common;
using System.Data.SqlClient;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    /// <summary>
    /// This activity creates a connection object for a SQL DB instance.
    /// </summary>
    [Name("SQL Connection Builder")]
    [Description("Creates a connection object for interacting with SQL instance.")]
    [WorkflowCategory("Database")]
    class SQLConnectionBuilder : Activity
    {
        #region Property Definitions

        public static DependencyProperty ServerNameProperty = DependencyProperty.Register("ServerName",
            typeof(string), typeof(SQLConnectionBuilder));

        /// <summary>
        /// Name of the DB Server.
        /// </summary>
        [RequiredInputParam]
        [Name("Database Server Name")]
        [Description("Name of the database server.")]
        public string ServerName
        {
            get { return ((string)(base.GetValue(SQLConnectionBuilder.ServerNameProperty))); }
            set { base.SetValue(SQLConnectionBuilder.ServerNameProperty, value); }
        }

        public static DependencyProperty DBNameProperty = DependencyProperty.Register("DBName",
            typeof(string), typeof(SQLConnectionBuilder));

        /// <summary>
        /// Name of the DB Server.
        /// </summary>
        [RequiredInputParam]
        [Name("Database Name")]
        [Description("Name of the database instance.")]
        public string DBName
        {
            get { return ((string)(base.GetValue(SQLConnectionBuilder.DBNameProperty))); }
            set { base.SetValue(SQLConnectionBuilder.DBNameProperty, value); }
        }

        public static DependencyProperty DatabaseConnectionProperty = DependencyProperty.Register("DatabaseConnection",
            typeof(SqlConnection), typeof(SQLConnectionBuilder));

        /// <summary>
        /// Database Server Connection Object.
        /// </summary>
        [OutputParam]
        [Name("Database Connection")]
        [Description("Connection to the database server.")]
        public SqlConnection DatabaseConnection
        {
            get { return ((SqlConnection)(base.GetValue(SQLConnectionBuilder.DatabaseConnectionProperty))); }
            set { base.SetValue(SQLConnectionBuilder.DatabaseConnectionProperty, value); }
        }
        
        #endregion

        /// <summary>
        /// Creates the connection object.
        /// </summary>
        /// <param name="executionContext"></param>
        /// <returns></returns>
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {

            string connStr = string.Format(@"Data Source={0},1433;Network Library=DBMSSOCN;Database={1};Trusted_Connection=yes;",
                                                    this.ServerName, this.DBName);

            SqlConnection conn = new SqlConnection(connStr);
            try
            {
                conn.Open();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Could not create connection to the database");
                Console.WriteLine(ex.ToString());
            }

            this.DatabaseConnection = conn;
            return ActivityExecutionStatus.Closed;
        }
    }
}
